package com.codebox;

import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.codebox.activity.AboutActivity;
import com.codebox.fragment.CodeListFragment;
import com.codebox.fragment.NavigationDrawerFragment;
import com.lib.EventBus;

public class MainActivity extends AppCompatActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

	private Toolbar toolbar;
	private DrawerLayout mDrawerLayout;

	private CodeListFragment mCodeLIstFragment;
	private NavigationDrawerFragment mNavigationDrawerFragment;
	public EventBus mEventBus;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mEventBus = EventBus.getInstance();
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.tl_custom);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_left);

        toolbar.setTitle("Toolbar");//设置Toolbar标题
        toolbar.setTitleTextColor(Color.parseColor("#ffffff")); //设置标题颜色
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer, mDrawerLayout,toolbar);
        mEventBus.registerEventHandler(0, mNavigationDrawerFragment);

        //列表内容
        mCodeLIstFragment = CodeListFragment.newInstance(Integer.MIN_VALUE);
        mEventBus.registerEventHandler(1, mCodeLIstFragment);
        getFragmentManager().beginTransaction().replace(R.id.container, mCodeLIstFragment).commit();
    }


    /*@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mEventBus = EventBus.getInstance();
		setContentView(R.layout.activity_main);
		// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
		mNavigationDrawerFragment.setUp(R.id.navigation_drawer, mDrawerLayout);
		mEventBus.registerEventHandler(0, mNavigationDrawerFragment);

		// UpdateChecker updateChecker = new UpdateChecker(this);
		// updateChecker.setCheckUrl("http://jcodecraeer.com/update.php");
		// updateChecker.checkForUpdates();

		mCodeLIstFragment = CodeListFragment.newInstance(Integer.MIN_VALUE);
		mEventBus.registerEventHandler(1, mCodeLIstFragment);
		getFragmentManager().beginTransaction().replace(R.id.container, mCodeLIstFragment).commit();
	}*/

	@SuppressLint("NewApi")
	@Override
	public void onNavigationDrawerItemSelected(int position) {
		final FragmentManager fragmentManager = getFragmentManager();
		final Handler handler = new Handler();
		switch (position) {
		case 0:
			if (mCodeLIstFragment == null) {

			}
			handler.postDelayed(new Runnable() {
				@Override
				public void run() {

				}
			}, 500);
			break;
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			mEventBus.sendEvent(EventBus.EventType.TOGGLE, null);
			break;
		case R.id.about:
			Intent intent = new Intent(MainActivity.this, AboutActivity.class);
			startActivity(intent);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (!mNavigationDrawerFragment.isDrawerOpen()) {
			// Only show items in the action bar relevant to this screen
			// if the drawer is not showing. Otherwise, let the drawer
			// decide what to show in the action bar.
			getMenuInflater().inflate(R.menu.main, menu);
			// restoreActionBar();
			return true;
		}
		return super.onCreateOptionsMenu(menu);
	}

}
